module.exports = (sequelize, Sequelize) => {
  const User = sequelize.define('User', {
    id: {
      type: Sequelize.INTEGER,
      autoIncrement: true,
      primaryKey: true
    },
    power: {
      type: Sequelize.INTEGER,
      defaultValue: 0
    },
    username: {
      type: Sequelize.STRING(50),
      allowNull: false
    },
    password: {
      type: Sequelize.STRING(50),
      allowNull: false,
      set(value) {
        const md5 = require('md5');
        this.setDataValue('password', md5(value));
      }
    },
    email: {
      type: Sequelize.STRING(50),
      allowNull: true
    },
    nickname: {
      type: Sequelize.STRING(50),
      allowNull: true
    },
    gender: {
      type: Sequelize.ENUM('male', 'female', 'other'),
      allowNull: true,
      defaultValue: "other",
      get() {
        const date = this.getDataValue('gender')
        switch (date) {
          case "male":
            return "男"
          case "female":
            return "女"
          case "other":
              return "未设置"
          default:
            return "未设置";
        }
      }
    },
    avatar: {
      type: Sequelize.STRING(255),
      allowNull: true,
      defaultValue: "default_avatar.jpg"
    },
    bio: {
      type: Sequelize.STRING(255),
      allowNull: true,
      defaultValue: "这人很懒，啥都没写"

    },
    wxopen_id: {
      type: Sequelize.STRING(50),
      allowNull: true,
    },
    created_at: {
      type: Sequelize.DATE,
      allowNull: false,
      defaultValue: Sequelize.NOW,
      get() {
        const date = this.getDataValue('created_at')
        return date ? date.toISOString().slice(0, 10) : null
      }
    },
    updated_at: {
      type: Sequelize.DATE,
      allowNull: false,
      defaultValue: Sequelize.NOW,
      get() {
        const date = this.getDataValue('updated_at')
        return date ? date.toISOString().slice(0, 10) : null
      }
    }
  }, {
    timestamps: false,
    underscored: true,
    tableName: 'user'
  })
  return User
} 